<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { contentApi } from '@/api/controller';
  import { getHMMSS } from '@/utils';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import xmImage from '@/components/xmImage/index.vue'
  import Pagination from "@/components/Pagination/index.vue";
  import backForm from './backForm.vue';
  const backFormRef = ref();
  const action = reactive({
    total:0,
    list:[] as any[],
    pageType:[] as Array<any>,
  })
  const listQuery = reactive({
    page: 1,
    limit: 20,
  })
  const handelOpenForm = (row:any = {})=>{
    backFormRef.value.openDialog({
      ...row,
      pageType:action.pageType
    })
  }
  const handleDelete = (row:any = {})=>{
    ElMessageBox.confirm("确定删除此背景墙吗?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      const loading = Toastloading();
      await contentApi.roomBackDel({
        id:row.id
      });
      loading.close();
      ToastSuccess("删除成功!");
      getList();
    })
    .catch(() => {});
  }
  const getPageType = async()=>{
    const res = await contentApi.getBackPageTypes()
    const { data=[] } = res
    action.pageType = data
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await contentApi.getRoomBackList(listQuery)
    loading.close()
    const { list=[], total=0 } = res.data
    action.list = list
    action.total = total
  }
  onMounted(()=>{
    getPageType()
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <el-button type="primary" @click="handelOpenForm()">新增</el-button>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column width="180" prop="img" label="展示位置">
        <template #default="scope">
          {{ action.pageType.find(res=>res.id == scope.row.type)?.value || '' }}
        </template>
      </el-table-column>
      <el-table-column width="100" prop="is_show" label="是否展示">
        <template #default="scope">
          {{ ['不显示','显示'][scope.row.is_show] }}
        </template>
      </el-table-column>
      <el-table-column min-width="180" prop="img" label="图片">
        <template #default="scope">
          <xmImage class="image" :src="scope.row.resource_url" :list="[scope.row.resource_url]" />
        </template>
      </el-table-column>
      <el-table-column width="100" prop="weight" label="展示权重" />
      <el-table-column min-width="280" prop="is_show" label="展示时间">
        <template #default="scope">
          {{ getHMMSS(scope.row.start_time * 1000 ) }}-{{ getHMMSS(scope.row.end_time * 1000) }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handelOpenForm(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status!='deleted'" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <backForm ref="backFormRef" @update-list="getList" />
  </div>
</template>
<style lang="scss" scoped>
  .image {
    height: 100px;
  }
</style>